﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Microsoft.Web.Data.UI.WebControls;
using System.Collections;
using Microsoft.Win32.SafeHandles;
using System.Threading;
using FlexiCommerce.Components;
using System.Web.DynamicData;

namespace FlexiCommerce.Web.UI.WebControls
{
    public class DomainDataSource : System.Web.DomainServices.WebControls.DomainDataSource, IHierarchicalDataSource
    {

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            
            
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.DomainServiceTypeName = typeof(FlexiDomainService).FullName;
            try
            {
                this.ItemType = this.GetTable().EntityType;
            }
            catch (Exception)
            {

            }
        }

        private Type ItemType
        {
            get { return (Type)ViewState["ItemType"]; }
            set { ViewState["ItemType"] = value; }
        }

        #region IHierarchicalDataSource Members

        public event EventHandler DataSourceChanged;

        public HierarchicalDataSourceView GetHierarchicalView(string viewPath)
        {

            if (ItemType == typeof(Category))
            {
            }
            if(ItemType == typeof(Category))
            {
                if (string.IsNullOrEmpty(viewPath))
                    return new ItemHierarchicalDataSourceView(null);
                return new ItemHierarchicalDataSourceView(Find.Categories.SingleOrDefault(c => c.ID == int.Parse(ID)));
            }
            return null;            
        }

        #endregion
    }
}
